package com.zhihu_lvliangi_android.option.mycircle.parents;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.renderscript.Element;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.CureRecord;
import com.zhihu_lvliangi_android.domain.HealthRecord;
import com.zhihu_lvliangi_android.domain.HospitalRecord;
import com.zhihu_lvliangi_android.option.HomePageActivity;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.yiliao.CureRecordActivity;
import com.zhihu_lvliangi_android.option.yiliao.CureRecordDetailActivity;
import com.zhihu_lvliangi_android.option.yiliao.HealthRecordDetailActivity;
import com.zhihu_lvliangi_android.option.yiliao.HospitalRecordDetailActivity;
import com.zhihu_lvliangi_android.option.yiliao.adapter.CureRecordAndHospitalAdapter;
import com.zhihu_lvliangi_android.option.yiliao.adapter.HealthRecordAdapter;
import com.zhihu_lvliangi_android.option.zhengwu.AffairDetailActivity;
import com.zhihu_lvliangi_android.option.zhengwu.Category_Item_List_Activity;
import com.zhihu_lvliangi_android.util.Constant;

public class General_item extends ParentFragment {

	private List mDataRecords = new ArrayList();;
	private boolean isHealthRecords = false;
	private Activity attachActivity;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		attachActivity = activity;
	}
	@Override
	protected int getLayoutId() {
		return R.layout.general_listview_in_scrollview;
	}

	@Override
	protected void setupViews(View parentView) {
		ListView listView = (ListView) parentView.findViewById(R.id.general_listview_ListView);
		ListAdapter adapter;
		if (isHealthRecords) {
			 adapter = new HealthRecordAdapter(getActivity(), mDataRecords);
		}else {
			 adapter = new CureRecordAndHospitalAdapter(getActivity(),mDataRecords);
		}
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(itemClickListener);
	}

	@Override
	protected void initialized(Bundle savedInstanceState) {
	}

	@Override
	public void threadTask() {
		
	}

	public static General_item newInstance(
			List<? extends Serializable> list) {
		General_item fatherFragment_item = new General_item();
		Bundle bundle = new Bundle();
		bundle.putSerializable(Constant.GENERAL_SAVE_KEY, (Serializable) list);
		fatherFragment_item.setArguments(bundle);
		return fatherFragment_item;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setUseMain_scrollview(false);
		super.onCreate(savedInstanceState);
		mDataRecords = (List) getArguments().getSerializable(Constant.GENERAL_SAVE_KEY);
		if (mDataRecords!= null && mDataRecords.size() >0 &&mDataRecords.get(0) instanceof HealthRecord) {
			isHealthRecords = true;
			
		}
	}
	
	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			//查询
				Intent intent = null;
//				
				if (isHealthRecords) {
					intent = new Intent(getActivity(), HealthRecordDetailActivity.class);
					intent.putExtra(Constant.GENERAL_SAVE_KEY, (HealthRecord)mDataRecords.get(position));
					 startActivity(intent);
				}else {
					Object data  = mDataRecords.get(position);
					if (data instanceof CureRecord) {
						
						intent = new Intent(getActivity(), CureRecordDetailActivity.class);
						intent.putExtra(Constant.GENERAL_SAVE_KEY, (CureRecord)mDataRecords.get(position));
						 startActivity(intent);
					}else {
						intent = new Intent(getActivity(), HospitalRecordDetailActivity.class);
						intent.putExtra(Constant.GENERAL_SAVE_KEY, (HospitalRecord)mDataRecords.get(position));
						 startActivity(intent);
					}
				}
				
		}
	};
}
